﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
//using Microsoft.Xna.Framework.Net;
//using Microsoft.Xna.Framework.Storage;

namespace SpriteDisplay
{
    /// <summary>
    /// Bullet klassen står för hur varje enskild pistolskula åker i den riktning som musen pekar.
    /// </summary>
    class Bullet
    {
        Vector2 position;

        Texture2D picture;

        float speed;
        float angle;
        Vector2 direction;
        float distance;
        float speed_Y;
        float speed_X;
        Vector2 mouse_pos;

        public Bullet(Texture2D bulletPicture, Vector2 startPosition, Vector2 updateMouse_pos, float updateAngle, float updateSpeed)
        {
            picture = bulletPicture;
            position = startPosition;
            speed = updateSpeed;
            angle = updateAngle;
            mouse_pos = updateMouse_pos;
            direction.X = Math.Abs(updateMouse_pos.X - startPosition.X);
            direction.Y = Math.Abs(updateMouse_pos.Y - startPosition.Y);
            distance = calculateDistance(updateMouse_pos, startPosition);
            speed_Y = speed * (direction.Y/(direction.X + direction.Y));
            speed_X = speed * (direction.X/(direction.X + direction.Y));
            
            if (mouse_pos.Y >= position.Y & mouse_pos.X >= position.X)
            {
                speed_Y = speed_Y + 0;
                speed_X = speed_X + 0;
            }
            else if (mouse_pos.Y >= position.Y & mouse_pos.X < position.X)
            {
                speed_Y = speed_Y + 0;
                speed_X = -speed_X;
            }
            else if (mouse_pos.Y < position.Y & mouse_pos.X >= position.X)
            {
                speed_Y = -speed_Y;
                speed_X = speed_X + 0;
            }
            else if (mouse_pos.Y < position.Y & mouse_pos.X < position.X)
            {
                speed_Y = -speed_Y;
                speed_X = -speed_X;
            }
        }

        public Vector2 Position { get { return position; } }

        public void Draw(SpriteBatch batch)
        {
            batch.Draw(picture, position, null, Color.White, angle,
                new Vector2(10.0f, 10.0f), 1.0f, SpriteEffects.None, 1.0f);
        }

        public void Update(GameTime gameTime)
        {
            
            position.Y += speed_Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
            position.X += speed_X * (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        private float calculateDistance(Vector2 A, Vector2 B)
        {
            //A == mouse_pos, B == position
            //Math.Abs(-1) will output 1
            //Math.Abs(1) will output 1
            //If the player_pos is negative make it positive
            A = new Vector2(Math.Abs(A.X), Math.Abs(A.Y));
            B = new Vector2(Math.Abs(B.X), Math.Abs(B.Y));

            float Y_diff, X_diff, distance;

            Y_diff = A.Y - B.Y;
            X_diff = A.X - B.X;

            //We have calculated the X difference and Y difference
            //Now apply Pythagorean theorem to calculate the distance
            distance = (float)Math.Sqrt(Math.Pow(X_diff, 2) + Math.Pow(Y_diff, 2));

            return (float)1;
        }
    }
}
